<html>
<head><meta charset="utf-8"><title>Reference in ptr::write question · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Reference.20in.20ptr.3A.3Awrite.20question.html">Reference in ptr::write question</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220857835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Reference%20in%20ptr%3A%3Awrite%20question/near/220857835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kitsu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Reference.20in.20ptr.3A.3Awrite.20question.html#220857835">(Dec 24 2020 at 09:01)</a>:</h4>
<p>I've found quite a strange thing in <code>ptr::write</code> source: <a href="https://doc.rust-lang.org/src/core/ptr/mod.rs.html#875">https://doc.rust-lang.org/src/core/ptr/mod.rs.html#875</a>. In particular, I'm curious about <code>&amp;mut *dst</code> part and how does it handle a pointer to uninitialized memory, so consider the following example:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">MaybeUninit</span>::<span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span>::<span class="n">uninit</span><span class="p">();</span><span class="w"></span>
<span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">().</span><span class="n">write</span><span class="p">(</span><span class="mh">0x12</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>As far as I know, ref must always reference a properly initialized object. And in the <code>ptr::write</code> the pointer for uninitialized memory is dereferenced passing a mutable ref. So the unsafe block should be similar to <code>let y = &amp;mut *x; intrinsics::move_val_init(y, 0x12);</code>. Does it mean that either <code>ptr::write</code> has an undefined behavior (due to <code>y</code> creation is invalid), or it just exploits some internal knowledge regarding rvalue ref behavior?</p>



<a name="220858392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Reference%20in%20ptr%3A%3Awrite%20question/near/220858392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Reference.20in.20ptr.3A.3Awrite.20question.html#220858392">(Dec 24 2020 at 09:14)</a>:</h4>
<p>You may want to ask at <a class="stream" data-stream-id="136281" href="/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines">#t-lang/wg-unsafe-code-guidelines</a>.</p>



<a name="220858926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Reference%20in%20ptr%3A%3Awrite%20question/near/220858926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kitsu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Reference.20in.20ptr.3A.3Awrite.20question.html#220858926">(Dec 24 2020 at 09:27)</a>:</h4>
<p>Didn't find that stream, thanks for the hint, duped question there.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>